﻿Imports System.Data.OleDb

Public Class AddNewRestaurant
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btn_Submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Submit.Click
        Dim newFileName As String
        newFileName = Guid.NewGuid().ToString() & getSuffix(fup_RestaurantPicture.FileName)
        If fup_RestaurantPicture.HasFile Then
            fup_RestaurantPicture.SaveAs(Server.MapPath("pictures") & "/" & newFileName)
        End If

        Dim newFileName2 As String
        newFileName2 = Guid.NewGuid().ToString() & getSuffix(fup_RestaurantPicture2.FileName)
        If fup_RestaurantPicture2.HasFile Then
            fup_RestaurantPicture2.SaveAs(Server.MapPath("pictures") & "/" & newFileName2)
        End If

        Dim newFileName3 As String
        newFileName3 = Guid.NewGuid().ToString() & getSuffix(FileUpload1.FileName)
        If FileUpload1.HasFile Then
            FileUpload1.SaveAs(Server.MapPath("Menu") & "/" & newFileName3)
        End If




        Dim sql As String = "INSERT INTO Restaurants (RestaurantName,Postcode,Website,RestaurantPicture,AveragePrice,PhoneNumber,MichelinStars,RestaurantAddress,AddedBy,RestaurantPicture2,RestaurantMenu) VALUES (@f1,@f2,@f3,f4,@f5,@f6,@f7,@f8,@f9,@f10,@f11)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", tb_RestaurantName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_postcode.Text)
        cmd.Parameters.AddWithValue("@f3", tb_website.Text)
        cmd.Parameters.AddWithValue("@f4", "pictures/" & newFileName)
        cmd.Parameters.AddWithValue("@f5", tb_AveragePrice.Text)
        cmd.Parameters.AddWithValue("@f6", tb_PhoneNumber.Text)
        cmd.Parameters.AddWithValue("@f7", rbl_MichelinStars.SelectedValue)
        cmd.Parameters.AddWithValue("@f8", tb_RestaurantAddress.Text)
        cmd.Parameters.AddWithValue("@f9", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f10", "pictures/" & newFileName2)
        cmd.Parameters.AddWithValue("@f11", "Menu/" & newFileName3)

        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
        Response.Redirect("list.aspx")

    End Sub

    Function getSuffix(ByVal fn As String)
        Dim fileParts = fn.Split(".")
        Dim suffix = fileParts(fileParts.Length - 1)
        Return "." & suffix
    End Function

    Protected Sub tb_RestaurantName_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles tb_RestaurantName.TextChanged

    End Sub

    Protected Sub tb_website_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles tb_website.TextChanged

    End Sub

    Protected Sub tb_postcode_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles tb_postcode.TextChanged

    End Sub
End Class